package com.szqhyxj.youxiangju.biz.personalInformation;


import com.szqhyxj.youxiangju.sys.BaseResult;

import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

import static com.szqhyxj.youxiangju.sys.util.DesensitizationUtils.idEncrypt;

/**
 * Created by leung on 2018/10/30.
 */

public class PersonalInformationPresenter implements PersonalInformationContract.Presenter {

    private PersonalInformationContract.View view;
    private PersonalInformationModel.PersonalInformationEntity entity;

    public PersonalInformationPresenter(PersonalInformationContract.View view) {
        this.view = view;
        this.entity = new PersonalInformationModel.PersonalInformationEntity();
    }

    @Override
    public void onDestroy() {

    }


    @Override
    public void loadInformation() {
        PersonalInformationModel.getpersonalInformation()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResult<PersonalInformationModel.PersonalInformationEntity>>() {
                    @Override
                    public void accept(BaseResult<PersonalInformationModel.PersonalInformationEntity> personalInformationEntityBaseResult) throws Exception {
                        if (personalInformationEntityBaseResult.businessLogicSuccess()) {
                            entity = personalInformationEntityBaseResult.getData();
                            view.showUsername(personalInformationEntityBaseResult.getData().getUsername().toString());
                            String idcard = idEncrypt(personalInformationEntityBaseResult.getData().getIdCard().toString());
                            view.showIdcardno(idcard);
                            view.showInvitenamname(personalInformationEntityBaseResult.getData().getInviteName());
                            if (personalInformationEntityBaseResult.getData().getInsuranceType() == 0) {
                                view.showPolicying("投保中");
                            } else if (personalInformationEntityBaseResult.getData().getInsuranceType() == 1) {
                                view.showPolicying("保障中");
                            }else if (personalInformationEntityBaseResult.getData().getInsuranceType() == -1){
                                view.showPolicying("未投保");
                            }else{
                                view.showPolicying("");
                            }
                            view.showUserphone(personalInformationEntityBaseResult.getData().getUseraccount());

                        } else {
                            view.showToast(personalInformationEntityBaseResult.getMessage());
                        }


                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast(throwable.getMessage());
                    }
                });
    }

    @Override
    public void telePhone() {
        view.navToTelephone(entity.getPhone());
    }
}
